package com.pospi.pai.yunpos.login;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.gprinter.aidl.GpService;
import com.gprinter.io.GpDevice;
import com.gprinter.service.GpPrintService;
import com.kyleduo.switchbutton.SwitchButton;
import com.lany.sp.SPHelper;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.view.listener.OnCreateBodyViewListener;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dao.PayWayDao;
import com.pospi.dto.GoodsDto;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.BaseActivity;
import com.pospi.pai.yunpos.bh.SupGoodsListActivity;
import com.pospi.pai.yunpos.caigou.CgshListActivity;
import com.pospi.pai.yunpos.caigou.CountsListActivity;
import com.pospi.pai.yunpos.caigou.OverFlowListActivity;
import com.pospi.pai.yunpos.caigou.ReceiveBillListActivity;
import com.pospi.pai.yunpos.caigou.RefundBillListActivity;
import com.pospi.pai.yunpos.cash.PointActivity;
import com.pospi.pai.yunpos.cash.ScanActivity;
import com.pospi.pai.yunpos.diaob.DboOutListActivity;
import com.pospi.pai.yunpos.diaob.DboReceiveListActivity;
import com.pospi.pai.yunpos.more.RestOrderActivity;
import com.pospi.pai.yunpos.more.StatisticsActivity;
import com.pospi.pai.yunpos.vip.VipActivity;
import com.pospi.pai.yunpos.xs.OutBillListActivity;
import com.pospi.pai.yunpos.xs.SaleListActivity;
import com.pospi.service.ERPService;
import com.pospi.service.UpLoadService;
import com.pospi.util.App;
import com.pospi.util.CashierLogin_pareseJson;
import com.pospi.util.LogCatHelper;
import com.pospi.util.constant.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.xiaopan.android.preference.PreferencesUtils;

public class Main_Menu extends BaseActivity implements AdapterView.OnItemClickListener {
//    @Bind(R.id.iv_cashier)
//    ImageView ivCashier;
//    @Bind(R.id.tv_cashier)
//    TextView tvCashier;
//    @Bind(R.id.ll_cashier)
//    LinearLayout llCashier;
//    @Bind(R.id.iv_member)
//    ImageView ivMember;
//    @Bind(R.id.tv_member)
//    TextView tvMember;
//    @Bind(R.id.ll_poiselect)
//    LinearLayout llMember;
//    @Bind(R.id.iv_statistics)
//    ImageView ivStatistics;
//    @Bind(R.id.tv_statistics)
//    TextView tvStatistics;
//    @Bind(R.id.ll_statistics)
//    LinearLayout llStatistics;
//    @Bind(R.id.iv_shift)
//    ImageView ivShift;
//    @Bind(R.id.tv_shift)
//    TextView tvShift;
//    @Bind(R.id.ll_shift)
//    LinearLayout llShift;
//    @Bind(R.id.login_sz)
//    ImageView loginSz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        ButterKnife.bind(this);



    }

    @OnClick({R.id.menu_sk, R.id.menu_hy, R.id.menu_bb, R.id.menu_yw,R.id.menu_sz})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu_sk:
                startActivity(PointActivity.class);
                break;
            case R.id.menu_hy:
                startActivity(VipActivity.class);
                break;
            case R.id.menu_bb:
                startActivity(StatisticsActivity.class);
                break;
            case R.id.menu_yw:
                showSelect();
                break;
            case R.id.menu_sz:
                break;
        }
    }


    //单选赠品
    private DialogFragment dialogFragment;

    private void showSelect() {
        dialogFragment = new CircleDialog.Builder(this)
                .setBodyView(R.layout.dialogyw, new OnCreateBodyViewListener() {
                    @Override
                    public void onCreateBodyView(View view) {
                        initDialog(view);
                    }
                })
                .setWidth(0.5f)
                .show();
    }
    private void initDialog(View view) {
        GridView gridview_main = view.findViewById(R.id.gridview_main);
        String[] from={"img","text"};
        int[] to={R.id.img,R.id.text};
        initData();
        adapter=new SimpleAdapter(this, dataList, R.layout.grid_item, from, to);
        gridview_main.setAdapter(adapter);
        gridview_main.setOnItemClickListener(this);
    }
    private SimpleAdapter adapter;
    private List<Map<String, Object>> dataList;
    private void initData() {
        //图标
        int icno[] = {R.drawable.icon_cgdd,R.drawable.icon_cgsh,R.drawable.icon_cgtc,
                R.drawable.icon_spzl,R.drawable.icon_spsy,R.drawable.icon_tbck,R.drawable.icon_tbrk,
                R.drawable.icon_xsdd,R.drawable.icon_xsck,R.drawable.icon_xsth,
                R.drawable.icon_mdbh,R.drawable.icon_sspd};
        //图标下的文字
        String name[]={
                "采购订单","采购收货","采购退厂",
                "商品资料","商品损溢","调拨出库",
                "调拨入库",
                "销售订单","销售出库","销售退货",
                "门店补货","实时盘点"};
        dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("img", icno[i]);
            map.put("text",name[i]);
            dataList.add(map);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (dataList.get(position).get("text").toString()) {
            case "门店补货":
                startActivity(SupGoodsListActivity.class);
                break;
            case "调拨出库":
                startActivity(DboOutListActivity.class);
                break;
            case "调拨入库":
                startActivity(DboReceiveListActivity.class);
                break;
            case "采购订单":
                startActivity(CgshListActivity.class);
                break;
            case "采购收货":
                startActivity(ReceiveBillListActivity.class);
                break;
            case "采购退厂":
                startActivity(RefundBillListActivity.class);
                break;
            case "销售订单":
                startActivity(SaleListActivity.class);
                break;
            case "销售出库":
                startActivity(OutBillListActivity.class);
                break;
            case "销售退货":
                startActivity(com.pospi.pai.yunpos.xs.RefundBillListActivity.class);
                break;
            case "商品损溢":
                startActivity(OverFlowListActivity.class);
                break;
            case "实时盘点":
                startActivity(CountsListActivity.class);
                break;
        }
    }
    //下载门店
    private void downStore() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login",Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.STORE_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }
}


